const express = require('express')
const cors = require('cors')
const userRoutes = require('./routes/userRoutes')
require('dotenv').config()

const app = express()

// 修改 CORS 配置
app.use(cors({
  origin: ['http://localhost:5173', 'http://127.0.0.1:5173'],
  credentials: true,
  methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
  allowedHeaders: ['Content-Type', 'Authorization']
}))

app.use(express.json())

// 路由
app.use('/api/users', userRoutes)

// 错误处理
app.use((err, req, res, next) => {
  console.error(err.stack)
  res.status(500).json({ message: '服务器错误' })
})

const PORT = process.env.PORT || 8080
app.listen(PORT, () => {
  console.log(`服务器运行在 http://localhost:${PORT}`)
}) 